package flc.ast.adapter;


import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyCollectBean;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class AlbumPicAdapter extends StkProviderMultiAdapter<MyCollectBean> {


    public AlbumPicAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new AlbumPicProvider());
    }

    private boolean isManage;

    public void setManage(boolean manage) {
        isManage = manage;
        notifyDataSetChanged();
    }

    private class AlbumPicProvider extends BaseItemProvider<MyCollectBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_album_pic;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyCollectBean bean) {
            if (bean.getPath().isEmpty()) {
                baseViewHolder.getView(R.id.ivAlbumPicItemAdd).setVisibility(View.VISIBLE);
                baseViewHolder.getView(R.id.rlAlbumPicItem).setVisibility(View.GONE);
            } else {
                baseViewHolder.getView(R.id.ivAlbumPicItemAdd).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.rlAlbumPicItem).setVisibility(View.VISIBLE);
                Glide.with(getContext()).load(bean.getPath()).into((ImageView) baseViewHolder.getView(R.id.ivAlbumPicItemImg));
                if (isManage) {
                    baseViewHolder.getView(R.id.ivAlbumPicItemSel).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.ivAlbumPicItemCollect).setVisibility(View.GONE);
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivAlbumPicItemSel, R.drawable.icon_gx2);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivAlbumPicItemSel, R.drawable.icon_gx1);
                    }
                } else {
                    baseViewHolder.getView(R.id.ivAlbumPicItemSel).setVisibility(View.GONE);
                    List<MyCollectBean> listSp = AppSpUtils.getCollectList();
                    if (listSp != null && listSp.size() > 0) {
                        if (listSp.contains(new MyCollectBean(bean.getPath(), false))) {
                            baseViewHolder.getView(R.id.ivAlbumPicItemCollect).setVisibility(View.VISIBLE);
                        } else {
                            baseViewHolder.getView(R.id.ivAlbumPicItemCollect).setVisibility(View.GONE);
                        }
                    } else {
                        baseViewHolder.getView(R.id.ivAlbumPicItemCollect).setVisibility(View.GONE);
                    }
                }
            }
        }
    }

}
